home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 July: Mac OS SDK / Dev.CD Jul 96 SDK / Dev.CD Jul 96 SDK1.toast / Development Kits (Disc 1) / OpenDoc / OpenDoc Development / Debugging Support / OpenDoc Source Code / Memory / PlatfMem.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1996-04-22  |  4.5 KB  |  176 lines  |  [TEXT/MPS ]

  1. /*
  2.     File:        PlatfMem.cpp
  3.  
  4.     Contains:    Platform layer implementation
  5.  
  6.     Owned by:    Michael Burbidge
  7.  
  8.     Copyright:    © 1993-95 by Apple Computer, Inc., all rights reserved.
  9.  
  10.     Change History (most recent first):
  11.     
  12.          <7>      5/4/95    jpa        Leave a min amount of free space in
  13.                                     platform heap. [1235657]
  14.          <6>    10/24/94    jpa        Strip address returned from
  15.                                     PlatformAllocateBlock. [1193659]
  16.          <5>     9/14/94    jpa        Eliminated dependencies on rest of OpenDoc.
  17.                                     [1186692]
  18.          <4>     8/17/94    jpa        Zap segments when freed, and check for
  19.                                     errors [1181509]
  20.          <3>      8/8/94    jpa        OD_DEBUG --> ODDebug
  21.          <2>     6/10/94    MB        Make it build
  22.          <1>      6/9/94    MB        first checked in
  23.          <3>     5/26/94    MB        #1162181: Fixed MMM integration bug
  24.          <2>      5/9/94    MB        #1162181: Changes necessary to install MMM.
  25.          <1>     4/29/94    MB        first checked in
  26.     To Do:
  27.     In Progress:
  28.         
  29. */
  30.  
  31. #ifndef _PLATFMEM_
  32. #include "PlatfMem.h"
  33. #endif
  34.  
  35. #ifndef _MEMMGRPV_
  36. #include "MemMgrPv.h"
  37. #endif
  38.  
  39. #ifndef __ERRORS__
  40. #include <Errors.h>
  41. #endif
  42.  
  43. #include <string.h>
  44. #include <stdio.h>
  45. #include <stdarg.h>
  46.  
  47.  
  48. //========================================================================================
  49. // Global function definitions
  50. //========================================================================================
  51.  
  52. //----------------------------------------------------------------------------------------
  53. // PlatformZapMem
  54. //----------------------------------------------------------------------------------------
  55.  
  56. void PlatformZapMem( void *start, size_t size, MMULong value )
  57. {
  58.     MM_ASSERT(((size_t)start&3)==0);                // Must start on a 4byte boundary
  59.     long *blk = (long *) start;
  60.     for (long i = size>>2; i>0; i--)                     // (i must be signed!)
  61.         *blk++ = value;
  62.     if( size&3 )
  63.         PlatformCopyMemory(&value,blk, size&3);        // Fill remaining 1-3 bytes
  64. }
  65.  
  66. //----------------------------------------------------------------------------------------
  67. // PlatformAllocateBlock
  68. //----------------------------------------------------------------------------------------
  69.  
  70. void *PlatformAllocateBlock(size_t size, MMHeapLocation src )
  71. {
  72.     // Check total free space before trying anything; must leave some amount free.
  73.     size_t free;
  74.     MMSystemFreeSpace(src,&free,kMMNULL);
  75.     if( free-size < kPlatformMinFreeSpace )
  76.         return kMMNULL;
  77.         
  78.     Handle handle = (Handle) MMAllocateHandleIn(size,src);
  79.             
  80.     if ( handle ) {
  81.         if( src==kMMAppMemory )
  82.             ::MoveHHi(handle);    // Jeff Crawford sez we don't need MoveHHi for temp/sys heap
  83.         ::HLock(handle);
  84.         OSErr err = MemError();
  85.         
  86.         if (err == noErr)
  87.             return StripAddress(*handle);
  88.         else
  89.             DisposeHandle(handle);
  90.     }
  91.     
  92.     return kMMNULL;
  93. }
  94.  
  95. //----------------------------------------------------------------------------------------
  96. // PlatformFreeBlock
  97. //----------------------------------------------------------------------------------------
  98.  
  99. void PlatformFreeBlock(void *ptr)
  100. {
  101.     Handle handle = ::RecoverHandle((Ptr) ptr);
  102.  
  103. #if MM_DEBUG
  104.     OSErr err = MemError();
  105.     if( err )
  106.         MM_WARN("PlatformFreeBlock got err %hd on ptr %p",err,ptr);
  107. #endif
  108.  
  109.     if( handle == NULL )
  110.         return;
  111.     
  112. #if MM_DEBUG
  113.     // Zap the block as we dispose it:
  114.     if( gValidate > 0 )
  115.         PlatformZapMem(ptr,::GetHandleSize(handle),0xDEADBEEF);
  116. #endif
  117.  
  118.     ::DisposeHandle(handle);
  119.     
  120. #if MM_DEBUG
  121.     err = MemError();
  122.     if( err && err != memFullErr )
  123.         MM_WARN("PlatformFreeBlock got err %hd on hdl %p",err,handle);
  124. #endif
  125. }
  126.  
  127. //----------------------------------------------------------------------------------------
  128. // BREAK
  129. //----------------------------------------------------------------------------------------
  130.  
  131. static void
  132. BREAK( const char msg[] )
  133. {
  134.     if( gHaveSOM )
  135.         somPrintf("%s\n",msg);
  136.     
  137.     // Now convert to Pascal string and call DebugStr:
  138.     Str255 pstr;
  139.     long len = strlen(msg);
  140.     pstr[0] = (len>255) ?255 :len;
  141.     for( int i=pstr[0]; i>0; i-- ) {
  142.         char c = msg[i-1];
  143.         if( c==';' ) c=':';        // ";" is bad in DebugStrs
  144.         pstr[i] = c;
  145.     }
  146.     DebugStr(pstr);
  147. }
  148.  
  149.  
  150. //----------------------------------------------------------------------------------------
  151. // MM_ASSERT_FAILED
  152. //----------------------------------------------------------------------------------------
  153.  
  154. void MM_ASSERT_FAILED(const char *cond, const char *fil)
  155. {
  156.     char dbg[256];
  157.     sprintf(dbg,"MM: %s ...NOT! In %s", cond,fil); 
  158.     BREAK(dbg);
  159. }
  160.  
  161. //----------------------------------------------------------------------------------------
  162. // MM_SHOW_WARNING
  163. //----------------------------------------------------------------------------------------
  164.  
  165. void MM_SHOW_WARNING(const char *msg, ...)
  166. {
  167.     char buf[512];
  168.     strcpy(buf, "MMWarning: ");
  169.     va_list args;
  170.     va_start(args,msg);
  171.     vsprintf(buf+strlen(buf),msg,args);
  172.     va_end(args);
  173.     
  174.     BREAK(buf);
  175. }
  176.